package io;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Arrays;

/**
 * 字节输出流测试：所有电脑上的文件都是字节
 */
public class FileOutPutStreamTest {
    public static void main(String[] args) {
        writeOneByte();
//        writeBytes();
//        writeBytesByLength();
//        writeBytesAppend();
    }

    /**
     * 写入一个字节
     */
    static void writeOneByte() {
        FileOutputStream fos = null;
        try {
            fos = new FileOutputStream("/Users/luchuan/IdeaProjects/java-test/a.txt");
//            String path = null;
//            FileOutputStream fos = new FileOutputStream(path); //如果传入的为空会抛空指针
            fos.write(97); // ASCII表对应的字符是a，此步骤会立即将字节输入到文件中
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fos != null) {
//            fos.flush();
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }
    }

    /**
     * 写入多个字节
     */
    static void writeBytes() {
        FileOutputStream fos = null;
        try {
            fos = new FileOutputStream("/Users/luchuan/IdeaProjects/java-test/a.txt");
//            fos.write(new byte[]{-12,99,122});

            byte[] bytes = "你好".getBytes();
            System.out.println("Arrays.toString(bytes) = " + Arrays.toString(bytes));
            fos.write(bytes);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fos != null) {
//            fos.flush();
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }
    }

    /**
     * 写入规定长度字节
     */
    static void writeBytesByLength() {
        FileOutputStream fos = null;
        try {
            fos = new FileOutputStream("/Users/luchuan/IdeaProjects/java-test/a.txt");
//            fos.write(new byte[]{-12,99,122});

            byte[] bytes = "你好".getBytes();
            System.out.println("Arrays.toString(bytes) = " + Arrays.toString(bytes));
            fos.write(bytes, 0, 3); //输出 '你' off 是指针开始的位置
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fos != null) {
//            fos.flush();
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }
    }

    /**
     * 写入规定长度字节
     */
    static void writeBytesAppend() {
        FileOutputStream fos = null;
        try {
            fos = new FileOutputStream("/Users/luchuan/IdeaProjects/java-test/a.txt", true);
//            fos.write(new byte[]{-12,99,122});

            byte[] bytes = "你好".getBytes();
            System.out.println("Arrays.toString(bytes) = " + Arrays.toString(bytes));
            fos.write("\r\n".getBytes());
            fos.write(bytes); //输出 '你' off 是指针开始的位置
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fos != null) {
//            fos.flush();
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }
    }
}
